package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.PageResult;
import com.itheima.service.OrdersService;
import com.itheima.vo.OrdersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * author:zfm
 * date:2022/6/5
 * description:订单查询
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 订单分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public PageResult pageOrder(Integer page, Integer pageSize,
                                @RequestParam(required = false) Long number,
                                @RequestParam(required = false) String beginTime,
                                @RequestParam(required = false) String endTime) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime begin = null;
        LocalDateTime end = null;
        if (beginTime != null){
            begin = LocalDateTime.parse(beginTime, timeFormatter);
        }
        if (endTime != null){
            end = LocalDateTime.parse(endTime, timeFormatter);
        }
        Page<OrdersVo> ordersVoPage = ordersService.page(page, pageSize, number, begin, end);
        if (ordersVoPage==null){
            return new PageResult(Code.ERROR,MessageConstant.GET_ORDER_ERROR,null);
        }
        long total = ordersVoPage.getTotal();
        return new PageResult(Code.OK, MessageConstant.GET_ORDER_SUCCESS, ordersVoPage.getRecords(), total);
    }
}
